package org.jground.rx.ui.zk.viewControl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.RX;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.util.ArrayUtils;
import org.jground.util.StringUtils;
import org.zkforge.ckez.CKeditor;
import org.zkoss.zk.ui.Component;

public class UIAttributeControlHtmltext extends UIAttributeControlBase {

    public static String CUSTOM_CONFIGURATION_PATH = "/js/fckconfig.js";

    private static Log logger = LogFactory.getLog(UIAttributeControlHtmltext.class);

    CKeditor editor = null;

    public UIAttributeControlHtmltext() {
        super();
    }

    private UIAttributeControlHtmltext(UIFrame uiFrame, RXView rxControl) {

        super(uiFrame, rxControl);

        // editor.setToolbar("Simple");
        // editor.setParent(popupWindow);

        //
        // String toolbarSet =
        // rxControl.getPropertyWithDefault(RX.AttName.toolbarSet,
        // RX.AttValue.basic);

        editor = new CKeditor();
        // editor.setCustomConfigurationsPath(CUSTOM_CONFIGURATION_PATH);
        // editor.setToolbar("Default");
        editor.setParent(this);
        String width = rxControl.getPropertyWithDefault(RX.AttName.width, "800px");
        String height = rxControl.getPropertyWithDefault(RX.AttName.height, "400px");
        editor.setWidth(width);
        editor.setHeight(height);

        add(editor);
    }

    public IUIAttributeView<Component, UIFrame> createNew(UIFrame uiFrame, RXView rxControl) {
        return new UIAttributeControlHtmltext(uiFrame, rxControl);
    }

    public void set(String... values) {
        clear();
        try {
            if (ArrayUtils.isNotEmpty(values) && StringUtils.isNotBlank(values[0])) {
                String value = values[0];
                editor.setValue(value);
            }
        } catch (Exception e) {
            logger.warn(e, e);
        }

    }

    public String[] get() {
        try {
            String s = editor.getValue();
            return ArrayUtils.toArray(s);
        } catch (Exception e) {
            return StringUtils.EMPTY_STRING_ARRAY;
        }

    }

    public void clear() {
        editor.setValue("");
    }

}
